from orm import Model, StringField, IntegerField, FloatField, TextField
import datetime,  time, random
import yydrm


def get_timestamp():
    return int(time.time())

i = 1
def create_orderid():
    global i
    t = datetime.datetime.now().strftime('%Y%m%d')
    r = str(i).zfill(2)
    i += 1
    return int(t + str(random.randint(10,99)) + r)


class user(Model):
    __table__ = 'users'

    user_id = StringField(primary_key=True,  ddl='varchar(50)')
    email = StringField(ddl='varchar(50)')
    nickname = StringField(ddl='varchar(30)')
    password = StringField(ddl='varchar(40)')
    balance = IntegerField(default = 0)
    status = IntegerField(default = -1)  #-1不可用，0正常
    create_at = IntegerField(default=get_timestamp)

class order_item(Model):
    __table__ = 'orders'

    order_id = IntegerField(primary_key = True,default = create_orderid)
    prepaid_for = StringField(ddl='varchar(50)')  #充值给
    payor = StringField(ddl='varchar(50)', default = 'admin')  #赠送为admin， 自己充值此字段与充值给相同。
    card_key = StringField(ddl = 'varchar(50)')
    amount = IntegerField()
    friend_id = StringField(ddl='varchar(50)', default = '')
    create_at = IntegerField(default=get_timestamp)


class PrepaidCard(Model):
    __table__ = 'prepaidCards'

    card_key = StringField(primary_key = True, ddl = 'varchar(50)')
    amount = IntegerField()
    create_at = IntegerField(default = get_timestamp)
    status = IntegerField(default = 0)  #0未使用，1已使用

